local sk__jiyang = fk.CreateSkill {

  name = "sk__jiyang",

  tags = { Skill.Compulsory, },

}



sk__jiyang:addEffect(fk.EventAcquireSkill, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and data.skill.name == "sk__jiyang" and not player.dead
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "@sk__jiyang", 3)
  end,
})
sk__jiyang:addEffect(fk.EventLoseSkill, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and data.skill.name == "sk__jiyang" and not player.dead
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = player:getMark("@sk__jiyang")
    room:setPlayerMark(player, "@sk__jiyang", 0)
    if n > 0 then
      local cards = room:getCardsFromPileByRule(".|.|heart,diamond", n, "drawPile")
      if #cards > 0 then
        room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonDraw, sk__jiyang.name, nil, false, player)
      end
    end
  end,
})
sk__jiyang:addEffect(fk.AfterCardsMove, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__jiyang.name) and player:getMark("@sk__jiyang") > 0 then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and
                Fk:getCardById(info.cardId).color == Card.Red then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = 0
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and
              Fk:getCardById(info.cardId).color == Card.Red then
            n = n + 1
          end
        end
      end
    end
    for i = 1, n, 1 do
      if player.dead or player:getMark("@sk__jiyang") == 0 then return end
      local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1,
        "#sk__jiyang-choose", sk__jiyang.name, true)
      if #to > 0 then
        room:removePlayerMark(player, "@sk__jiyang", 1)
        to = room:getPlayerById(to[1])
        if to:isWounded() then
          room:recover({
            who = to,
            num = 1,
            recoverBy = player,
            skillName = sk__jiyang.name,
          })
        else
          room:changeMaxHp(to, 1)
        end
      else
        return
      end
    end
  end,
})

return sk__jiyang
